from flask import Flask
from flask import request
import json
import os
import uuid
import cv2
 
app = Flask(__name__)
 
@app.route("/")
def index():
    return "Hello world"
 
resSets = {}
@app.route('/postdata', methods=['POST'])
def postdata():
    f = request.files['content']
    user_input = request.form.get("name")
    basepath = os.path.dirname(__file__)  # 当前文件所在路径
    src_imgname = str(uuid.uuid1()) + ".jpg"
    upload_path = os.path.join(basepath, 'static/srcImg/')
    if os.path.exists(upload_path)==False:
        os.makedirs(upload_path)
    f.save(upload_path + src_imgname)
    img = cv2.imread(upload_path + src_imgname, 1)
    save_path = os.path.join(basepath, 'static/resImg/')
    if os.path.exists(save_path) == False:
        os.makedirs(save_path)
    # Using cv2.putText() method 
    img = cv2.putText(img, 'OpenCV', (40, 60), cv2.FONT_HERSHEY_SIMPLEX , 2, (255, 0, 0), 2, cv2.LINE_AA)
    # 用绿色(0, 255, 0)来画出矩形
    x, y, w, h = 10, 10, 300, 100
    img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
    save_imgname = str(uuid.uuid1()) + ".jpg"
    cv2.imwrite(save_path + save_imgname, img)
    resSets["value"] = 10
    resSets["resurl"] = "http://10.40.27.236:8090" +'/static/resImg/' + save_imgname
    return json.dumps(resSets, ensure_ascii=False)
 
if __name__=="__main__":
    app.run(host="0.0.0.0", port=8090)
